 layui.define(['form','selectN','table'], function(exports){
  var $ = layui.$,form = layui.form,selectN = layui.selectN,table = layui.table;

    var goodsId = $('input[name="id"]').val();
	var typeCodes = $('input[name="typeCodes"]').val();
    var token = $('input[name="marketResToken"]').val();
	
    $('input[name="imgs"]').jfFileManager({
        resDomain:layui.setter.resDomain,
        token:token,
        multiple:true,
        parent:parent
    });

    $('input[name="thumbnailImg"]').jfFileManager({
        resDomain:layui.setter.resDomain,
        token:token,
        multiple:false,
        parent:parent
    });

    $('input[name="detailImgs"]').jfFileManager({
        resDomain:layui.setter.resDomain,
        token:token,
        multiple:true,
        parent:parent
    });
	
	$('select[name="classify"]').change(function(){
		console.log('1');
	});
	
	selectN({
        elem: '#types'
        , name: 'types'
        , search: [true, true]
        , selected: typeCodes
        , data: layui.setter.webRoot +'/market/goodsType/tree/select.data'
        , field: {
            idName: 'code',
            titleName: 'name',
            statusName: 'status',
            childName: 'children'
        }
    });
	
	$('#projectId').select2({
        placeholder: "请选择商城项目",
        width: '300px',
        theme: 'bootstrap',
        allowClear : true,
        ajax: {
            url: layui.setter.webRoot +'/market/project/select.data',
            method: 'post',
            delay: 250,
            processResults: function (result) {
                if (result.code == 200) {
                    return {
                        pagination: {more: result.body.more},
                        results: result.body.data || []
                    };
                } else {
                    return {results: []};
                }
            }
        }
    });

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/market/project/goods/space/list.data'
        ,where:{goodsId:goodsId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,size:'lg'
        ,cols: [[
            {field: 'id', width: 100, title: 'ID', sort: true}
            ,{field: 'name', title: '商品名称', minWidth: 100}
            ,{field: 'thumbnailImg', title: '规格图片', width: 100, templet: function (record){
                if(record.thumbnailImg){
                    return '<img style="display: inline-block; width: 100%; height: 100%;" src="'+record.thumbnailImg+'">';
                }else{
                    return '---';
                }
            }}
            ,{field: 'stateName',title: '状态'}
            ,{field: 'createDate', title: '创建时间', sort: true}
            ,{title: '操作', width: 150, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
        ]]
        ,page: false
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    //事件
    var active = {
        addSpace: function () {
            layer.open({
                type: 2
                , title: '新增商品规格'
                , content: layui.setter.webRoot + '/device/goods/space/add.html?goodsId='+goodsId
                , maxmin: true
                , area: ['500px', '500px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-form-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);
                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/device/goods/space/add.do',
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    table.reload('LAY-table'); //数据刷新
                                    layer.close(index); //关闭弹层
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,updateSpace: function (obj) {
            layer.open({
                type: 2
                , title: '修改商品规格'
                , content: layui.setter.webRoot + '/device/goods/space/update.html?goodsId='+goodsId+'&id=' + obj.data.id
                , maxmin: true
                , area: ['500px', '500px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-form-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);
                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/device/goods/space/update.do',
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    table.reload('LAY-table'); //数据刷新
                                    layer.close(index); //关闭弹层
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });

                    submit.trigger('click');
                }
            });
        }
        , delSpace: function (obj) {
            layer.confirm('确定要删除该商品规格吗？', function (index) {
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/device/goods/space/del.do',
                    data: {goodsId:goodsId,id: obj.data.id},
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 200) {
                            obj.del();
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    }

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });
  
  exports('goods_edit', {})
});